package com.ssms.service;

import com.ssms.commons.Result;
import com.ssms.commons.TablePage;
import com.ssms.dao.AcademyMapper;
import com.ssms.dao.ClazzMapper;
import com.ssms.dao.SubjectMapper;
import com.ssms.model.Academy;
import com.ssms.model.Clazz;
import com.ssms.model.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

@Service
public class AcademyService {

    @Resource
    private AcademyMapper academyMapper;
    
    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private ClazzMapper clazzMapper;

    public Result save(Academy academy) {
        Result check = check(academy);
        if (check != null) {
            return check;
        }

        int count = academyMapper.insertSelective(academy);
        return count > 0?Result.success():Result.failed("添加失败", 500);
    }


    public TablePage search(TablePage tablePage, String val) {
        int size = tablePage.getLimit();
        int start = (tablePage.getPage() - 1) * size;
        // 获取分页数据
        List<Academy> roles = academyMapper.selectPage(start, size, val);
        // 获取符合条件的总数
        int count = academyMapper.selectPageCount(val);
        tablePage.setCount(count);
        tablePage.setData(roles);
        return tablePage;
    }

    // 校验院系名和编号不能重复
    private Result check(Academy academy){
        Academy obj = academyMapper.selectByName(academy.getAcademyName());
        if (obj != null && academy.getId() != obj.getId()) {
            return Result.failed("院系名称已存在", 500);
        }

        obj = academyMapper.selectByNo(academy.getAcademyNo());
        if (obj != null && academy.getId() != obj.getId()) {
            return Result.failed("院系编号已存在", 500);
        }

        return null;
    }

    public Result update(Academy academy) {
        Result check = check(academy);
        if (check != null) {
            return check;
        }

        int count = academyMapper.updateByPrimaryKeySelective(academy);
        return count > 0?Result.success():Result.failed("更新失败", 500);
    }

    public Result delete(Integer id) {
        List<Subject> subjects = subjectMapper.selectByAcademyId(id);
        if(subjects != null && subjects.size() > 0){
            return Result.failed("该院系下存在专业，不能删除", 500);
        }

        int count = academyMapper.deleteByPrimaryKey(id);
        return count > 0?Result.success():Result.failed("删除失败", 500);
    }

    public Result all() {
        return Result.success(academyMapper.selectAll());
    }

    // 获取院系专业树形列表
    public List<HashMap<String, Object>> getAcademyTree(){
        List<HashMap<String,Object>> academyTreeList = new ArrayList<>();
        // 获取所有院系
        List<Academy> academyList = academyMapper.selectAll();
        for (Academy academy : academyList) {
            HashMap<String,Object> item = new HashMap<>();
            item.put("title",academy.getAcademyName());
            item.put("field","academy");
            item.put("id",academy.getId());
            // 获取对应院系下的所有专业
            List<Subject> subjectList = subjectMapper.selectByAcademyId(academy.getId());
            List<HashMap<String,Object>> childrens = new ArrayList<>();
            // 将专业添加到对应的院系对象下
            for (Subject subject : subjectList) {
                HashMap<String,Object> subjectItem = new HashMap<>();
                subjectItem.put("title",subject.getSubjectName());
                subjectItem.put("field","subject");
                subjectItem.put("id",subject.getId());
                childrens.add(subjectItem);
                item.put("children",childrens);
            }
            academyTreeList.add(item);
        }

        return academyTreeList;
    }

    // 携带班级信息
    public List<HashMap<String, Object>> getAcademyTree2(){
        List<HashMap<String, Object>> academyTree = getAcademyTree();
        for (HashMap<String, Object> root : academyTree) {
            List<HashMap<String,Object>> subjects = (List<HashMap<String, Object>>) root.get("children");
            if(subjects == null){
                continue;
            }

            for (HashMap<String, Object> subject : subjects) {
                List<Clazz> clazzList = clazzMapper.selectAllBySubjectId((Integer) subject.get("id"));
                if(clazzList == null){
                    continue;
                }
                List<HashMap<String,Object>> childrens = new ArrayList<>();
                for (Clazz clazz : clazzList) {
                    HashMap<String,Object> subjectItem = new HashMap<>();
                    subjectItem.put("title",clazz.getClazzName());
                    subjectItem.put("field","clazz");
                    subjectItem.put("id",clazz.getId());
                    childrens.add(subjectItem);
                    subject.put("children",childrens);
                }
            }
        }

        return academyTree;
    }

}
